Passed
Push — master ( 0c90fa...d64b6a )
by Kevin Van
01:43 queued 12s
created

helper.ts ➔ mapPsdStatus   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
rs 9.95
c 0
b 0
f 0
cc 1
1
export function mapMatchStatus(statusCode) {
2
  const statusCodes = new Map([
3
    [`PP`, `Uitgesteld`],
4
    [`ST`, `Stopgezet`],
5
    [`F1`, `Forfait`],
6
    [`FI`, `Forfait`],
7
    [`F2`, `Forfait`],
8
    [`FF`, `Forfait`],
9
    [`AMC`, `Algemeen forfait`],
10
  ])
11
12
  return statusCodes.get(statusCode) || null
13
}
14
15
/**
16
 * Strip first character if the division matches a youth division structure AND starts with a number.
17
 *
18
 * General form:
19
 *  - G9E
20
 *  - 2G10G
21
 *  - P7L
22
 *  - 2P12M
23
 * The function should return true if it matches the form starting with a "2".
24
 *
25
 * @param {string} division
26
 */
27
export function isYouthDivisionWithNumericFirst(division) {
28
  return /^(\d+)([a-zA-Z]+)(\d*)([a-zA-Z])/.test(division)
29
}
30
31
/**
32
 * Remove the first character of a division string if it's a number.
33
 *
34
 * Numbers are sometimes added for youth divisions to indicate a second period within a same season.
35
 * For example, G9K is the regional U9 division K before new year's day. From January 1st, the teams
36
 * can be re-arranged in a new (more balanced) division, which will be named something like 2G2K,
37
 * with the "2" in front indicating this difference.
38
 *
39
 * @param {string} division
40
 */
41
export function replaceFirstCharIfNumber(division) {
42
  if (isYouthDivisionWithNumericFirst(division)) {
43
    // Remove first character.
44
    division = division.substr(1)
45
  }
46
47
  return division
48
}
49
50
/**
51
 * Convert a region+division into an output label.
52
 *
53
 * @param {array} divisionArray
54
 * @param {string} level
55
 */
56
export function outputDivision(divisionArray, level = ``) {
57
  if (divisionArray[0] === `BCA`) {
58
    return `Beker van Brabant`
59
  } else if (divisionArray[0] === `ESCA`) {
60
    return `Beker voor B-ploegen`
61
  } else if (divisionArray[0] === `FR`) {
62
    return `Vriendschappelijk`
63
  } else if (divisionArray[0] === `BVZ`) {
64
    return `Beker van Zemst`
65
  } else if (divisionArray[2] <= 4) {
66
    return `${divisionArray[2]}e ${level !== `nat` ? `Prov.` : `Nationale`} ${divisionArray[3]}`
67
  } else {
68
    return `U${divisionArray[2]} / ${divisionArray[3]}${divisionArray[4] ? ` / ${divisionArray[4]}` : ``}`
69
  }
70
}
71
72
/**
73
 * Replace a divisionCode with its descriptive label.
74
 *
75
 * @param {string} division
76
 */
77
export function mapDivision(division) {
78
  return /^([A-Z]+)?(\d+)?([a-zA-Z]+)(\d*)$/.exec(replaceFirstCharIfNumber(division))
79
}
80
81
/**
82
 * Retrieve mapping and the formatted descriptive label of a division.
83
 *
84
 * @param {string} division
85
 * @param {string} region
86
 */
87
export function formatDivision(division, region) {
88
  const divisionArr = mapDivision(division)
89
  return outputDivision(divisionArr, region)
90
}
91
92
/**
93
 * Truncate to <n> letters and optionally stop at the last word instead of letter.
94
 *
95
 * @param {int} size
96
 * @param {boolean} useWordBoundary
97
 */
98
export function truncate(size, useWordBoundary = true) {
99
  if (this.length <= size) {
100
    return this
101
  }
102
  const subString = this.substr(0, size - 1)
103
  return (useWordBoundary ? subString.substr(0, subString.lastIndexOf(` `)) : subString) + `…`
104
}
105
106
/**
107
 * Map a positionCode to a descriptive label.
108
 *
109
 * @param {string} positionCode
110
 */
111
export function mapPositionCode(positionCode) {
112
  return getPositions().get(positionCode) || null
113
}
114
115
/**
116
 * List of all positions, in order of position on the fields.
117
 *
118
 * @param {string} positionCode
119
 */
120
export function getPositions() {
121
  const positions = new Map([
122
    [`k`, `Doelman`],
123
    [`d`, `Verdediger`],
124
    [`m`, `Middenvelder`],
125
    [`a`, `Aanvaller`],
126
  ])
127
  return positions
128
}
129
130
export function mapPsdStatus(statusCode) {
131
  const statusCodes = new Map([
132
    [0, `Gepland`],
133
    [1, `Forfait`],
134
    [2, `Afgelast`],
135
    [3, `Onderbroken`],
136
  ])
137
138
  return statusCodes.get(statusCode) || null
139
}
140
141
export function mapPsdStatusShort(statusCode) {
142
  const statusCodes = new Map([
143
    [0, ``],
144
    [1, `FF`],
145
    [2, `AFG`],
146
    [3, `STOP`],
147
  ])
148
149
  return statusCodes.get(statusCode) || null
150
}
151
152
export function mapPsdStatusIcon(statusCode) {
153
  const statusCodes = new Map([
154
    [0, ``],
155
    [1, `fa-times`],
156
    [2, `fa-times`],
157
    [3, `fa-ban`],
158
  ])
159
160
  return statusCodes.get(statusCode) || null
161
}
162
163
export function translateGameResult(result) {
164
  const statusCodes = new Map([
165
    [`WON`, `Gewonnen`],
166
    [`EQUAL`, `Gelijkgespeeld`],
167
    [`LOST`, `Verloren`],
168
  ])
169
  return statusCodes.get(result) || null
170
}
171
172
export function capitalizeFirstLetter(string) {
173
  return string.charAt(0).toUpperCase() + string.slice(1)
174
}
175
176
export function groupByDate(data) {
177
  const groups = data.reduce((groups, object) => {
178
    const date = object.start.split(` `)[0]
179
    if (!groups[date]) {
180
      groups[date] = []
181
    }
182
    groups[date].push(object)
183
    return groups
184
  }, {})
185
186
  // Edit: to add it in the array format instead
187
  const groupArrays = Object.keys(groups).map((date) => {
188
    return {
189
      date,
190
      objects: groups[date],
191
    }
192
  })
193
194
  return groupArrays
195
}
196
197
export function groupByMonth(data) {
198
  const groups = data.reduce((groups, object) => {
199
    const date = new Date(object.timestamp * 1000)
200
    const month = date.toLocaleString(`nl-BE`, { month: `long` })
201
    if (!groups[month]) {
202
      groups[month] = []
203
    }
204
    groups[month].push(object)
205
    return groups
206
  }, {})
207
208
  const groupArrays = Object.keys(groups).map((date) => {
209
    return {
210
      date,
211
      objects: groups[date],
212
    }
213
  })
214
215
  return groupArrays
216
}
217
218
export function replaceAll(source: string, search: string, replacement: string) {
219
  return source.replace(new RegExp(search, `g`), replacement)
220
}
221
222
export function sortRankings(a: RankingDataTeamObject, b: RankingDataTeamObject) {
223
  // Rank lager: A stijgt in sortering.
224
  if (a.rank < b.rank) {
225
    return -1
226
  }
227
  if (a.rank > b.rank) {
228
    return 1
229
  }
230
  // Aantal overwinningen hoger: A stijgt in sortering.
231
  if (a.wins > b.wins) {
232
    return -1
233
  }
234
  if (a.wins < b.wins) {
235
    return 1
236
  }
237
  // Doelpuntensaldo beter: A stijgt in sortering.
238
  if (a.goalsScored - a.goalsConceded > b.goalsScored - b.goalsConceded) {
239
    return -1
240
  }
241
  if (a.goalsScored - a.goalsConceded < b.goalsScored - b.goalsConceded) {
242
    return 1
243
  }
244
  // Aantal gemaakte doelpunten hoger: A stijgt in sortering.
245
  if (a.goalsScored > b.goalsScored) {
246
    return -1
247
  }
248
  if (a.goalsScored < b.goalsScored) {
249
    return 1
250
  }
251
  // Aantal uitoverwinningen hoger: A stijgt in sortering.
252
  if (a.winsAway > b.winsAway) {
253
    return -1
254
  }
255
  if (a.winsAway < b.winsAway) {
256
    return 1
257
  }
258
  // Doelpuntensaldo op verplaatsing beter: A stijgt in sortering.
259
  if (a.goalsScoredAway - a.goalsConcededAway > b.goalsScoredAway - b.goalsConcededAway) {
260
    return -1
261
  }
262
  if (a.goalsScoredAway - a.goalsConcededAway < b.goalsScoredAway - b.goalsConcededAway) {
263
    return 1
264
  }
265
  // Aantal gemaakte doelpunten op verplaatsing hoger: A stijgt in sortering.
266
  if (a.goalsScoredAway > b.goalsScoredAway) {
267
    return -1
268
  }
269
  if (a.goalsScoredAway < b.goalsScoredAway) {
270
    return 1
271
  }
272
273
  return a.team?.club?.localName.localeCompare(b.team?.club?.localName)
274
}
275
276
export default {
277
  mapMatchStatus,
278
  mapDivision,
279
  formatDivision,
280
  truncate,
281
  mapPositionCode,
282
  getPositions,
283
  mapPsdStatus,
284
  mapPsdStatusShort,
285
  mapPsdStatusIcon,
286
  translateGameResult,
287
  capitalizeFirstLetter,
288
  groupByDate,
289
  groupByMonth,
290
  replaceAll,
291
  sortRankings,
292
}
293